package com.ccc.scala.base7_collection

import scala.collection.mutable.ArrayBuffer


object Collection03_FoldNScan {
  def main(args: Array[String]): Unit = {
    // 集合 - Fold Scan

    val array = ArrayBuffer(1, 2, 3, 4, 5)
    val num = 5

    val res1 = array.fold(5)(_ - _)
    println(array.scan(5)(_ - _)) // ArrayBuffer(5, 4, 2, -1, -5, -10)
    println(res1) // 20

    // 1 - (2 - (3 - (4 - (5 - 5))))
    val res2 = array.foldRight(5)(_ - _)
    println(res2) // -2
    // println(1 - (2 - (3 - (4 - (5 - 5))))) // -2
    println(array.scanRight(5)(_ - _)) // ArrayBuffer(-2, 3, -1, 4, 0, 5)

  }

}
